<?php
/**
 * 阿里支付
 */
use \conf\DbConf;
$alipayLibPath = dirname(__FILE__) . '/../library/ThirdParty/Alipay';
include_once $alipayLibPath . '/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';
include_once $alipayLibPath . '/pagepay/service/AlipayTradeService.php';
include_once $alipayLibPath . '/pagepay/buildermodel/AlipayTradeRefundContentBuilder.php';
include_once $alipayLibPath . '/pagepay/buildermodel/AlipayTradeQueryContentBuilder.php';

class AlipayController extends Yaf_Controller_Abstract
{
    private $config;

    public function init()
    {
        $this->config = DbConf::ALIPAY_CONFIG;
    }

    public function indexAction(){
        return true;
    }

    public function paybillAction()
    {
        //商户订单号，商户网站订单系统中唯一订单号，必填
        $out_trade_no = trim($_POST['WIDout_trade_no']);

        //订单名称，必填
        $subject = trim($_POST['WIDsubject']);

        //付款金额，必填
        $total_amount = trim($_POST['WIDtotal_amount']);

        //商品描述，可空
        $body = trim($_POST['WIDbody']);

        //构造参数
        $payRequestBuilder = new AlipayTradePagePayContentBuilder();
        $payRequestBuilder->setBody($body);
        $payRequestBuilder->setSubject($subject);
        $payRequestBuilder->setTotalAmount($total_amount);
        $payRequestBuilder->setOutTradeNo($out_trade_no);

        $aop = new AlipayTradeService($this->config);

        /**
         * pagePay 电脑网站支付请求
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @param $return_url 同步跳转地址，公网可以访问
         * @param $notify_url 异步通知地址，公网可以访问
         * @return $response 支付宝返回的信息
         */
        $response = $aop->pagePay($payRequestBuilder, $this->config['return_url'], $this->config['notify_url']);

        //输出表单
        var_dump($response);
        return false;
    }

    public function billqueryAction()
    {
        require_once dirname(__FILE__) . '/buildermodel/AlipayTradeQueryContentBuilder.php';
        //商户订单号，商户网站订单系统中唯一订单号
        $out_trade_no = trim($_POST['WIDTQout_trade_no']);

        //支付宝交易号
        $trade_no = trim($_POST['WIDTQtrade_no']);
        //请二选一设置
        //构造参数
        $RequestBuilder = new AlipayTradeQueryContentBuilder();
        $RequestBuilder->setOutTradeNo($out_trade_no);
        $RequestBuilder->setTradeNo($trade_no);

        $aop = new AlipayTradeService($this->config);

        /**
         * alipay.trade.query (统一收单线下交易查询)
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @return $response 支付宝返回的信息
         */
        $response = $aop->Query($RequestBuilder);
        var_dump($response);
        return false;
    }

    public function refundAction()
    {
        require_once dirname(__FILE__) . '/buildermodel/AlipayTradeRefundContentBuilder.php';
        //商户订单号，商户网站订单系统中唯一订单号
        $out_trade_no = trim($_POST['WIDTRout_trade_no']);

        //支付宝交易号
        $trade_no = trim($_POST['WIDTRtrade_no']);
        //请二选一设置

        //需要退款的金额，该金额不能大于订单金额，必填
        $refund_amount = trim($_POST['WIDTRrefund_amount']);

        //退款的原因说明
        $refund_reason = trim($_POST['WIDTRrefund_reason']);

        //标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
        $out_request_no = trim($_POST['WIDTRout_request_no']);

        //构造参数
        $RequestBuilder = new AlipayTradeRefundContentBuilder();
        $RequestBuilder->setOutTradeNo($out_trade_no);
        $RequestBuilder->setTradeNo($trade_no);
        $RequestBuilder->setRefundAmount($refund_amount);
        $RequestBuilder->setOutRequestNo($out_request_no);
        $RequestBuilder->setRefundReason($refund_reason);

        $aop = new AlipayTradeService($this->config);

        /**
         * alipay.trade.refund (统一收单交易退款接口)
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @return $response 支付宝返回的信息
         */
        $response = $aop->Refund($RequestBuilder);
        var_dump($response);
        return false;
    }
}
